package com.nulstudio.emotion.text.controller;

import com.nulstudio.emotion.text.common.NulResult;
import com.nulstudio.emotion.text.entity.NulInvite;
import com.nulstudio.emotion.text.request.GenerateTokenRequest;
import com.nulstudio.emotion.text.service.InviteService;
import org.springframework.lang.NonNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/invite")
public class InviteController {
    @NonNull
    private final InviteService inviteService;

    public InviteController(@NonNull InviteService inviteService) {
        this.inviteService = inviteService;
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/generate")
    public NulResult<String> generate(@RequestBody @NonNull GenerateTokenRequest request) {
        final NulInvite invite = inviteService.generateInvite(request.expireTime(), request.role(),
                request.remaining(), request.blocked());
        return NulResult.response(invite.getCode());
    }

}
